package exceptions

import (
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	zhs "github.com/go-playground/validator/v10/translations/zh"
	"regexp"
)

var (
	//validate = validator.New()          // 实例化验证器
	chinese  = zh.New()                 // 获取中文翻译器
	uni      = ut.New(chinese, chinese) // 设置成中文翻译器
	trans, _ = uni.GetTranslator("zh")  // 获取翻译字典
)

func Translate(validate *validator.Validate, err error) (errRet map[string]string) {
	_ = zhs.RegisterDefaultTranslations(validate, trans)

	if e, ok := err.(validator.ValidationErrors); ok {
		// 翻译，并返回
		//s.errRet(c, 500, errors.Translate(trans))
		errRet = e.Translate(trans)
		return
	}
	return nil
}

func IsValidUUID(u string) bool {
	r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
	return r.MatchString(u)
}
